package com.group2.view;
import static model.netBase.IP;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Objects;

import javax.swing.*;

import model.*;
public class StudentAddFrame extends  JFrame {
	JTextField name;
	JTextField number;
	JTextField gender;
	JTextField grade;
	JTextField birthplace;
	JTextField major;
	JTextField school;
//	JFrame jf1=new JFrame();
	JPanel panel1 = new JPanel(null);// 第一个JPanel
	// JComboBox id;


	public StudentAddFrame() {
		//super();
		init();
	}

	public void init() {
		this.setTitle("欢迎管理员来到学籍管理系统！");
		this.setSize(550, 450);
		this.setResizable(false);
		panel1.setBounds(0, 0, 550, 450);
		Font d = new Font("黑体", Font.BOLD, 17);
		Font f = new Font("黑体", Font.PLAIN, 15);
		
		JLabel icon = new JLabel();
		icon.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/admin.png")));
		icon.setBounds(50, 50, 30, 30);
		
		JLabel studenttitle = new JLabel("请您添加学生：");
		JLabel studentsname = new JLabel("姓名：");
		name = new JTextField();
		JLabel studentsnumber = new JLabel("学号：");
		number = new JTextField();
		JLabel studentssex = new JLabel("性别：");
		gender = new JTextField();
		JLabel studentsgrade = new JLabel("年级：");
		grade = new JTextField();
		JLabel studentsbirthplace = new JLabel("籍贯：");
		birthplace = new JTextField();
		JLabel studentsmajor = new JLabel("专业：");
		major = new JTextField();
		JLabel studentsschool = new JLabel("学院：");
		school = new JTextField();

		JButton submit = new JButton("确认");
		submit.setFocusPainted(false);
		JButton reset = new JButton("重置");
		reset.setFocusPainted(false);

		studenttitle.setBounds(100, 45, 200, 40);
		studenttitle.setFont(d);

		studentsname.setBounds(50, 100, 80, 20);
		studentsname.setFont(f);
		name.setBounds(100, 100, 100, 20);
		name.setFont(f);

		studentsnumber.setBounds(50, 140, 80, 20);
		studentsnumber.setFont(f);
		number.setBounds(100, 140, 100, 20);
		number.setFont(f);

		studentssex.setBounds(220, 140, 80, 20);
		studentssex.setFont(f);
		gender.setBounds(270, 140, 100, 20);
		gender.setFont(f);

		studentsgrade.setBounds(50, 180, 80, 20);
		studentsgrade.setFont(f);
		grade.setBounds(100, 180, 100, 20);
		grade.setFont(f);

		studentsbirthplace.setBounds(220, 180, 80, 20);
		studentsbirthplace.setFont(f);
		birthplace.setBounds(270, 180, 100, 20);
		birthplace.setFont(f);

		studentsmajor.setBounds(50, 220, 80, 20);
		studentsmajor.setFont(f);
		major.setBounds(100, 220, 100, 20);
		major.setFont(f);

		studentsschool.setBounds(220, 220, 80, 20);
		studentsschool.setFont(f);
		school.setBounds(270, 220, 100, 20);
		school.setFont(f);

		submit.setBounds(100, 280, 80, 30);
		submit.setFont(f);
		reset.setBounds(200, 280, 80, 30);
		reset.setFont(f);

		panel1.add(icon);
		panel1.add(studenttitle);
		panel1.add(studentsname);
		panel1.add(name);
		panel1.add(studentsnumber);
		panel1.add(number);
		panel1.add(studentssex);
		panel1.add(gender);
		panel1.add(studentsgrade);
		panel1.add(grade);
		panel1.add(studentsbirthplace);
		panel1.add(birthplace);
		panel1.add(studentsmajor);
		panel1.add(major);
		panel1.add(studentsschool);
		panel1.add(school);
		panel1.add(submit);
		panel1.add(reset);
		
		this.getContentPane().add(panel1);
		this.setVisible(true);
		// 居中显示
		this.setLocationRelativeTo(null);

		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				name.setText("");
				number.setText("");
				gender.setText("");
				grade.setText("");
				birthplace.setText("");
				major.setText("");
				school.setText("");
			}
		});
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (name.getText().equals("") || number.getText().equals("") || gender.getText().equals("")
						|| grade.getText().equals("") || birthplace.getText().equals("") || major.getText().equals("")
						|| school.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "请输入内容");
				}
				else {
					Student students = new Student();
					students.setName(name.getText());
					students.setNumber(number.getText());
					students.setSex(gender.getText());
					students.setGrade(grade.getText());
					students.setBirthplace(birthplace.getText());
					students.setMajor(major.getText());
					students.setSchool(school.getText());
					ArrayList<String> list = new ArrayList<>();
					students.setMyCourse(list);
					try {
						Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
						//设置超时时间
						socket.setSoTimeout(3000);
						System.out.println(students);
						//构建IO
						InputStream is = socket.getInputStream();
						OutputStream os = socket.getOutputStream();
						BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
						BufferedReader br = new BufferedReader(new InputStreamReader(is));
						//向服务器传输操作码
						bw.write("32" + '\n');
						bw.write(students.getName() + '\n');
						bw.write(students.getNumber() + '\n');
						bw.write(students.getSex() + '\n');
						bw.write(students.getGrade() + '\n');
						bw.write(students.getBirthplace() + '\n');
						bw.write(students.getMajor() + '\n');
						bw.write(students.getSchool() + '\n');
						bw.flush();
						//建立输入输出对象流
						System.out.println("传输完成");
						String flag = br.readLine();
						System.out.println(flag);
						socket.close();
						if (Objects.equals(flag, "true"))
							JOptionPane.showMessageDialog(null, "添加成功！");
						else
							JOptionPane.showMessageDialog(null, "添加失败！");
					} catch (Exception a) {
						System.out.println("连接异常！");
						JOptionPane.showMessageDialog(null, "添加失败！");
					}
				}

			}
		});
	}

	public static void main(String[] args) {
		new StudentAddFrame();
	}
}